package com.wxj.book.cms.controller;

import com.github.pagehelper.PageInfo;
import com.wxj.book.cms.common.ServerResponse;
import com.wxj.book.cms.pojo.Comment;
import com.wxj.book.cms.service.CommentService;
import com.wxj.book.cms.service.CommentVoService;
import com.wxj.book.cms.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CommentController {
    @Autowired
    private CommentVoService commentVoService;
    @Autowired
    private CommentService commentServeice;

    /**
     * 查询评价
     */
    @RequestMapping("comment/getAll")
    public String getAllComment(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                Model model) {
        PageInfo<CommentVo> pageInfo = commentVoService.getAll(pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return "comment";
    }

    /**
     * 更新评论状态
     */
    @PostMapping("comment/updateContent")
    @ResponseBody
    public ServerResponse updateContent(Comment comment) {
        int result = commentServeice.update(comment);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    /**
     * 根据id删除
     */
    @RequestMapping("comment/deleteById")
    @ResponseBody
    public ServerResponse deleteById(@RequestParam("commentId") Integer commentId) {
        if (commentId == null ||commentId.equals("") || commentId.equals(" ")) {
            return ServerResponse.createByErrorMessage("删除失败");
        }
        int result = commentServeice.deleteById(commentId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }
}
